﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.SystemManage
{
    public class UserService : DbHelperBase
    {
        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool IsExists(UserEntity model)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE LoginCode=@LoginCode ");
            var result = false;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (Convert.ToInt32(reader["count"]) > 0)
                            {
                                result = true;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Login(UserEntity model)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE LoginCode=@LoginCode AND LoginPwd=@LoginPwd");
            var result = false;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    Db.AddInParameter(cmd, "@LoginPwd", EncryptPassWord.ConvertStringToBase64String(model.LoginPwd, Encoding.UTF8));
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (Convert.ToInt32(reader["count"]) > 0)
                            {
                                result = true;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="loginCode"></param>
        /// <returns></returns>
        public UserEntity GetModel(string loginCode)
        {
            var model = new UserEntity();
            object aObj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT LoginCode,FullName,RoleID FROM UT_User WHERE LoginCode=@LoginCode ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", loginCode);

                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            model.LoginCode = reader["LoginCode"] == aObj ? null : Convert.ToString(reader["LoginCode"]);
                            model.FullName = reader["FullName"] == aObj ? null : Convert.ToString(reader["FullName"]);
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public int GetUserCount(UserQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.LoginCode))
            {
                strSql.Append(" AND LoginCode like '%" + qcmodel.LoginCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.FullName))
            {
                strSql.Append(" AND FullName like '%" + qcmodel.FullName + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.DptID))
            {
                strSql.Append(" AND DptID='" + qcmodel.DptID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RoleID))
            {
                strSql.Append(" AND RoleID='" + qcmodel.RoleID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" AND RecStatus='" + qcmodel.RecStatus + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.MobileNo))
            {
                strSql.Append(" AND MobileNo = '" + qcmodel.MobileNo + "'");
            }
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        /// <summary>
        /// 获取用户分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public List<UserEntity> GetUserList(UserQcEntity qcmodel)
        {
            var list = new List<UserEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;

            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by A.InputTime desc) AS idx,A.UserID,A.LoginCode,A.LoginPwd,A.FullName,A.DptID,C.DptName,A.RoleID,B.RoleName,A.MobileNo,A.RecStatus,A.[Desc],A.QCode FROM UT_User AS A  ");
            strSql.Append(" INNER JOIN UT_Role AS B ON A.RoleID=B.RoleID ");
            strSql.Append(" INNER JOIN UT_Department AS C ON A.DptID=C.DptID ");

            if (!string.IsNullOrEmpty(qcmodel.LoginCode))
            {
                strSql.Append(" AND A.LoginCode like '%" + qcmodel.LoginCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.FullName))
            {
                strSql.Append(" AND A.FullName like '%" + qcmodel.FullName + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.DptID))
            {
                strSql.Append(" AND A.DptID='" + qcmodel.DptID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RoleID))
            {
                strSql.Append(" AND A.RoleID='" + qcmodel.RoleID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" AND A.RecStatus='" + qcmodel.RecStatus + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.MobileNo))
            {
                strSql.Append(" AND A.MobileNo ='" + qcmodel.MobileNo + "'");
            }
            strSql.Append(") AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new UserEntity();
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.LoginPwd = reader["LoginPwd"] == obj ? null : Convert.ToString(reader["LoginPwd"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToInt64(reader["RecStatus"]);
                            model.Desc = reader["Desc"] == obj ? null : Convert.ToString(reader["Desc"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取单条用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserEntity GetUserInfo(string userId)
        {
            var model = new UserEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT UserID, LoginCode, LoginPwd, FullName, DptID, RoleID,[MobileNo],RecStatus,[Desc] FROM UT_User Where UserID=@UserID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                Db.AddInParameter(cmd, "@UserID", userId);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.LoginPwd = reader["LoginPwd"] == obj ? null : Convert.ToString(reader["LoginPwd"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToInt64(reader["RecStatus"]);
                            model.Desc = reader["Desc"] == obj ? null : Convert.ToString(reader["Desc"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddUser(UserEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO UT_User([UserID],[LoginCode],[LoginPwd],[FullName],[DptID],[RoleID],[MobileNo],[RecStatus],[Desc]) ");
            strSql.Append(" VALUES(@UserID,@LoginCode,@LoginPwd,@FullName,@DptID,@RoleID,@MobileNo,@RecStatus,@Desc) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UserID", model.UserID);
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    Db.AddInParameter(cmd, "@LoginPwd", EncryptPassWord.ConvertStringToBase64String(model.LoginPwd, Encoding.UTF8));
                    Db.AddInParameter(cmd, "@FullName", model.FullName);
                    Db.AddInParameter(cmd, "@DptID", model.DptID);
                    Db.AddInParameter(cmd, "@RoleID", model.RoleID);
                    Db.AddInParameter(cmd, "@MobileNo", model.MobileNo);
                    Db.AddInParameter(cmd, "@RecStatus", model.RecStatus);
                    Db.AddInParameter(cmd, "@Desc", model.Desc);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ModifyUser(UserEntity model)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_User SET [LoginCode] = @LoginCode,[FullName] = @FullName,[DptID] = @DptID,[RoleID] = @RoleID,[MobileNo]=@MobileNo,[Desc] = @Desc WHERE UserID = @UserID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    Db.AddInParameter(cmd, "@FullName", model.FullName);
                    Db.AddInParameter(cmd, "@DptID", model.DptID);
                    Db.AddInParameter(cmd, "@RoleID", model.RoleID);
                    Db.AddInParameter(cmd, "@MobileNo", model.MobileNo);
                    Db.AddInParameter(cmd, "@Desc", model.Desc);
                    Db.AddInParameter(cmd, "@UserID", model.UserID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool LockUser(string userId)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_User SET [RecStatus] = 0 WHERE UserID = @UserID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UserID", userId);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool UnlockUser(string userId)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_User SET [RecStatus] = 1 WHERE UserID = @UserID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UserID", userId);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public List<UserEntity> GetIsNullQCode()
        {
            var list = new List<UserEntity>();
            object aObj = DBNull.Value;
            const string sqlStr = " SELECT UserID,FullName FROM UT_User WHERE ISNULL(QCode, '')='' ";
            using (DbCommand cmd = Db.GetSqlStringCommond(sqlStr))
            {

                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new UserEntity();
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.FullName = reader["FullName"] == aObj ? null : Convert.ToString(reader["FullName"]);
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }

        public bool UpdateQCode(List<UserEntity> list)
        {
            string sql = @"UPDATE UT_User SET [QCode] = @QCode WHERE [UserID] = @UserID";
            sql = string.Format(sql);
            bool result = false;
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    Db.AddInParameter(cmd, "@QCode", DbType.String);
                    Db.AddInParameter(cmd, "@UserID", DbType.String);
                    foreach (var i in list)
                    {
                        cmd.Parameters["@QCode"].Value = PinYin.GetFirstPinyin(i.FullName).ToLower();
                        cmd.Parameters["@UserID"].Value = i.UserID;
                        if (Db.ExecuteNonQuery(cmd) > 0)
                        {
                            result = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool CheckedLoginCode(UserEntity model)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE LoginCode=@LoginCode ");
            var result = false;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (Convert.ToInt32(reader["count"]) > 0)
                            {
                                result = true;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }

        public bool CheckedRecStatus(UserEntity model)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE LoginCode=@LoginCode and RecStatus=1");
            var result = false;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (Convert.ToInt32(reader["count"]) > 0)
                            {
                                result = true;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }

        public bool ModifyPwd(UserEntity model)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_User SET [LoginPwd] = @LoginPwd WHERE LoginCode = @LoginCode ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", model.LoginCode);
                    Db.AddInParameter(cmd, "@LoginPwd", EncryptPassWord.ConvertStringToBase64String(model.LoginPwd, Encoding.UTF8));
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public int GetIsOperatorUserCount(UserQcEntity qcmodel)
        {
            var strSql = new StringBuilder();

            strSql.Append("select  count(*) AS [count] FROM UT_User AS A WHERE RoleID <> '11111111-1111-1111-1111-111111111111' ");
            if (!string.IsNullOrEmpty(qcmodel.LoginCode))
            {
                strSql.Append(" AND LoginCode like '%" + qcmodel.LoginCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.FullName))
            {
                strSql.Append(" AND FullName like '%" + qcmodel.FullName + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.DptID))
            {
                strSql.Append(" AND DptID='" + qcmodel.DptID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" AND RecStatus='" + qcmodel.RecStatus + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.MobileNo))
            {
                strSql.Append(" AND MobileNo ='" + qcmodel.MobileNo + "'");
            }
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public IList<UserEntity> GetIsOperatorUserList(UserQcEntity qcmodel)
        {
            var list = new List<UserEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;

            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by A.InputTime desc) AS idx,A.UserID,A.LoginCode,A.LoginPwd,A.FullName,A.DptID,C.DptName,A.RoleID,B.RoleName,A.MobileNo,A.RecStatus,A.[Desc] FROM UT_User AS A  ");
            strSql.Append(" INNER JOIN UT_Role AS B ON A.RoleID=B.RoleID ");
            strSql.Append(" INNER JOIN UT_Department AS C ON A.DptID=C.DptID ");
            strSql.Append(" WHERE A.RoleID <> '11111111-1111-1111-1111-111111111111' ");
            if (!string.IsNullOrEmpty(qcmodel.LoginCode))
            {
                strSql.Append(" AND A.LoginCode like '%" + qcmodel.LoginCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.FullName))
            {
                strSql.Append(" AND A.FullName like '%" + qcmodel.FullName + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.DptID))
            {
                strSql.Append(" AND A.DptID='" + qcmodel.DptID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" AND A.RecStatus='" + qcmodel.RecStatus + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.MobileNo))
            {
                strSql.Append(" AND A.MobileNo = '" + qcmodel.MobileNo + "'");
            }
            strSql.Append(") AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new UserEntity();
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.LoginPwd = reader["LoginPwd"] == obj ? null : Convert.ToString(reader["LoginPwd"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToInt64(reader["RecStatus"]);
                            model.Desc = reader["Desc"] == obj ? null : Convert.ToString(reader["Desc"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public UserEntity UserLogin(UserQcEntity qcmodel)
        {
            var model = new UserEntity();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT * FROM UT_User WHERE LoginCode=@LoginCode");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@LoginCode", qcmodel.LoginCode);
                    using (var reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.LoginPwd = reader["LoginPwd"] == obj ? null : Convert.ToString(reader["LoginPwd"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToInt64(reader["RecStatus"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }

        public IList<ExportUserEntity> ExportUser(UserQcEntity qcmodel)
        {
            var list = new List<ExportUserEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT A.RecStatus,A.LoginCode,A.FullName,A.MobileNo,B.RoleName,C.DptName,A.[Desc] FROM UT_User AS A  ");
            strSql.Append(" INNER JOIN UT_Role AS B ON A.RoleID=B.RoleID ");
            strSql.Append(" INNER JOIN UT_Department AS C ON A.DptID=C.DptID ");
            if (!string.IsNullOrEmpty(qcmodel.LoginCode))
            {
                strSql.Append(" AND A.LoginCode like '%" + qcmodel.LoginCode + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.FullName))
            {
                strSql.Append(" AND A.FullName like '%" + qcmodel.FullName + "%'");
            }
            if (!string.IsNullOrEmpty(qcmodel.DptID))
            {
                strSql.Append(" AND A.DptID='" + qcmodel.DptID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RoleID))
            {
                strSql.Append(" AND A.RoleID='" + qcmodel.RoleID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" AND A.RecStatus='" + qcmodel.RecStatus + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.MobileNo))
            {
                strSql.Append(" AND A.MobileNo ='" + qcmodel.MobileNo + "'");
            }
            strSql.Append(" order by A.InputTime desc ");

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ExportUserEntity();                            
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);                           
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            var rec = Convert.ToBoolean(reader["RecStatus"]);
                            model.RecStatus = rec ? "未锁定" : "已锁定";
                            model.Desc = reader["Desc"] == obj ? null : Convert.ToString(reader["Desc"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
